package com.vca.front.controller;

import com.vca.common.request.PaymentRequest;
import com.vca.common.response.PaymentPreOrderResponse;
import com.vca.common.response.UmsQueryResponse;
import com.vca.common.result.CommonResult;
import com.vca.common.utils.VcaUtil;
import com.vca.service.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:支付控制器
 * @author:chenbing
 * @date 2022/12/2 11:14
 */
@Slf4j
@RestController("PaymentController")
@RequestMapping("api/front/order/payment")
@Api(tags = "支付部分API")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    /**
     * @Description:支付预下单
     * @author:chenbing
     * @date 2022/12/2 11:31
     */
    @ApiOperation(value = "支付预下单")
    @PostMapping(value = "/pre/order")
    public CommonResult<PaymentPreOrderResponse> preOrder(
            @RequestHeader(value = "language",required = false) String language,
            @RequestBody PaymentRequest request, HttpServletRequest httpServletRequest
    ) {
        String ip = VcaUtil.getClientIp(httpServletRequest);
        return CommonResult.success(paymentService.preOrder(language,request, ip));
    }

    /**
     * @Description:查询支付结果
     * @author:chenbing
     * @date 2022/12/16 17:38
     */
    @ApiOperation(value = "查询支付结果")
    @PostMapping(value = "/query/status")
    public CommonResult<UmsQueryResponse> queryStatus(
            @RequestHeader(value = "language",required = false) String language,
            @RequestBody PaymentRequest request
    ) {
        return CommonResult.success(paymentService.queryStatus(language,request));
    }

    /**
    * @description 订单关闭
    * @param request
    * @author Li
    * @date 2023/6/8 9:47
    * @return {@link CommonResult}<{@link UmsQueryResponse}>
    */
    @ApiOperation(value = "订单支付关闭")
    @PostMapping(value = "/close")
    public CommonResult<UmsQueryResponse> close(
            @RequestBody PaymentRequest request
    ) {
        return Boolean.TRUE.equals(paymentService.close(request))?CommonResult.success():CommonResult.failed();
    }
}
